package cn.shop.dys.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import cn.shop.dys.R;
import cn.shop.dys.adapter.CxkbAdapter;
import cn.shop.dys.bean.Topic;
import cn.shop.dys.constant.CommonConstant;

/**
 * Created by liulu on 2016/4/24.
 */
public class CxkbActivity extends AppCompatActivity{

    private ListView cxkb;
    private ImageView goback;
    private TextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cxkb);
        cxkb = (ListView) findViewById(R.id.lv_cxkb);
        goback = (ImageView) findViewById(R.id.iv_common_back);
        title = (TextView) findViewById(R.id.tv_common_index_titile);
        initData();
    }

    private void initData() {
        requestData();
        title.setText("促销快报");
        goback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void requestData() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addQueryStringParameter("page", "1");
        params.addQueryStringParameter("pageNum","10");
        httpUtils.send(HttpRequest.HttpMethod.GET, CommonConstant.CXKB_URL, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                try {
                    String result = responseInfo.result;
                    JSONObject jsonObject = new JSONObject(result);
                    String jsonTopic = jsonObject.getString("topic");
                    Gson gson = new Gson();
                    List<Topic> topics = gson.fromJson(jsonTopic, new TypeToken<List<Topic>>() {
                    }.getType());
                    CxkbAdapter cxkbAdapter = new CxkbAdapter(CxkbActivity.this, topics);
                    cxkb.setAdapter(cxkbAdapter);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }
}
